<template>
    <div class="admin-management">
        <Search @searchData="getAdminData" @resetData="getAdminData" :placeholder="placeholder"></Search>
        <!-- <AddAndDel></AddAndDel> -->
        <el-card>
            <el-table ref="multipleTable" class="custom-table" height="450px"
                :default-sort="{ prop: 'date', order: 'descending' }"
                :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" :data="tableData" tooltip-effect="dark"
                style="width: 100%">
                <!-- <el-table-column prop="userId" label="序号" sortable>
                    <template slot-scope="scope">{{ scope.row.id }}</template>
                </el-table-column> -->
                <el-table-column prop="userAccount" label="账号" width="150" align="center">
                </el-table-column>
                <el-table-column prop="username" label="用户名">
                </el-table-column>
                <el-table-column prop="userTel" label="电话">
                </el-table-column>
                <el-table-column prop="email" label="邮箱" width="200">
                </el-table-column>
                <el-table-column prop="avatarUrl" label="头像" width="200">
                    <template slot-scope="scope">
                        <el-image class="img" :src="scope.row.avatarUrl" :preview-src-list="srcList"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="roleName" label="角色">
                </el-table-column>
                <!-- <el-table-column label="操作" align="center" width="200">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.row)" plain>编辑</el-button>
                        <el-button size="mini" type="danger" style="color:red" @click="handleDelete(scope.row)"
                            plain>删除</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <Pagination :paginationData="paginationData" @getPaginationData="getAdminData"></Pagination>
        </el-card>
        <!-- <AddAdminDialog @addAdmin="addAdmin"></AddAdminDialog> -->
        <!-- <EditAdminDialog @editAdmin="editAdmin"></EditAdminDialog> -->
    </div>
</template>
<script>
import Search from '../../components/search/index.vue'
import AddAndDel from '../../components/add_and_del/index.vue'
import Pagination from '../../components/pagination/index.vue'
import AddAdminDialog from './add_dialog/index.vue'
import EditAdminDialog from './edit_dialog/index.vue'

export default {
    name: 'AdminManagement',
    components: {
        Search,
        AddAndDel,
        Pagination,
        AddAdminDialog,
        EditAdminDialog
    },
    mixins: [],
    props: {

    },
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            paginationData: {},
            placeholder: '请输入账号查询',
            srcList: []
        }
    },
    computed: {

    },
    watch: {

    },
    mounted() {
        this.getAdminData()
    },
    methods: {
        // 获取管理员列表信息
        getAdminData(obj = { currentPage: 1, pageSize: 10, keyword: '' }) {
            this.request.post('/user/adminList', obj).then(res => {
                if (res.code == 20000) {
                    res.data.list.forEach(item => {
                        if (item.isSuperAdmin == 1) {
                            item.isSuperAdmin = 'ADMIN'
                        }
                    })
                    this.tableData = res.data.list
                    this.paginationData = res.data
                    this.srcList = res.data.list.map(item => item.avatarUrl)
                }
            })
        },
        // 新增管理员
        addAdmin(obj) {
            this.request.post('/user/adminAdd', obj).then(res => {
                if (res.code == 20000) {
                    // 重新获取管理员信息列表
                    this.getAdminData()
                    this.$message({
                        type: 'success',
                        message: '管理员新增成功！'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: res.msg
                    })
                }
            })
        },
        // 编辑管理员信息
        editAdmin(obj) {
            // /user/adminEdit
            this.request.put('/user/adminEdit', obj).then(res => {
                if (res.code == 20000) {
                    // 重新获取管理员信息列表
                    this.getAdminData()
                    this.$message({
                        type: 'success',
                        message: '编辑成功！'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: '编辑失败'
                    })
                }
            })
        },
        // 编辑管理员信息
        handleEdit(row) {
            this.$bus.$emit('edit', true)
            this.$bus.$emit('getForm', row)

        },
        // 删除管理员信息
        handleDelete(row) {
            this.$confirm('此操作将永久删除该管理员账号, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.request.delete('/user/adminDel?userId=' + row.userId).then(res => {
                    if (res.code == 20000) {
                        this.getAdminData()
                        this.$message({
                            type: 'success',
                            message: '删除成功！'
                        })

                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        }
    }
};
</script>
<style lang='less' scoped>
.admin-management {
    .el-table {
        font-size: 12px;
    }

    .img {
        width: 50px;
        height: 50px;
        border-radius: 50%;
    }

    .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #11A983 !important;
        color: #FFF;
    }

}
</style>